#include "EZ_Thread.h"


void tSetSysTickPeriod(uint32_t ms)
{
  SysTick->LOAD  = ms * SystemCoreClock / 1000 - 1; 
  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);
  SysTick->VAL   = 0;                           
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk; 
}

void system_init(void)
{
		MEM8(NVIC_SYSPRI2) = NVIC_PENDSV_PRI;  
		tSetSysTickPeriod(1);
}


void main_thread_entry(void *parameter)
{
  extern int $Super$$main(void);
	$Super$$main();
}
void thread_application_init(void)
{
	
	static uint32_t main_stack[EZ_MAIN_THREAD_STACK_SIZE];
	static ez_thread_t main_task;
	
	task_init(&main_task,main_thread_entry,(void *)0x0,&main_stack[1024],0,5);
	task_startup(&main_task);
	
}

int $Sub$$main(void)
{

	
	system_init();
	
	system_scheduler_init();
	
	sys_timer_init();
	
	idel_init();
	
	thread_application_init();
	
	system_scheduler_start();


	return 0;
}
